﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class MainCommitteeExpenseViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainCommitteeExpenseViewModel"/> class.
        /// </summary>
        /// <param name="adminReposiroty">The admin reposiroty.</param>
        /// <param name="financeService">The finance service.</param>
        public MainCommitteeExpenseViewModel(IAdministrationRepository adminReposiroty, IFinanceService financeService)
        {
            _AdministrationRepository = adminReposiroty;
            _FinanceService = financeService;
        }


        /// <summary>
        /// Gets the main branch expenses.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns></returns>
        public IEnumerable GetMainBranchExpenses(Guid projectID)
        {
            Guid bankAccountID = GetMainCommitteeBankAccountID(projectID);
            return from expense in _AdministrationRepository.FindMainCommitteeExpenses(commExpense => commExpense.CommitteBankAccountID.Equals(bankAccountID))
                   select new
                   {
                       ExpenseDate = expense.ExpenseDate,
                       Amount = expense.Amount,
                       ProjectName = expense.CommitteeBankAccount.Project.ProjectName
                   };
        }

        /// <summary>
        /// Gets the main committee bank account ID.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns></returns>
        private Guid GetMainCommitteeBankAccountID(Guid projectID)
        {
            var mainBranch = _AdministrationRepository.FindCommittees(comm => comm.IsMainCommittee == true).First();
            return mainBranch.CommitteeBankAccounts.Where(bankAcc => bankAcc.ProjectID.Equals(projectID)).First().CommitteeBankAccountID;
        }

        /// <summary>
        /// Gets the main branch balance.
        /// </summary>
        /// <returns></returns>
        public double GetMainBranchBalance()
        {
            return _FinanceService.GetMainCommitteeBalance();
        }

        /// <summary>
        /// Gets the user project ID.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetUserProjects()
        {
            return _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);
        }

        /// <summary>
        /// Withdraws from main committee.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <param name="amount">The amount.</param>
        /// <param name="expenseDate">The expense date.</param>
        public void WithdrawFromMainCommittee(Guid projectID, double amount, DateTime expenseDate)
        {
            using (IUnitOfWork trans = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                Guid bankAccID = GetMainCommitteeBankAccountID(projectID);

                IMainCommitteeExpense mainCommExpense = ServiceLocator.Current.GetInstance<IMainCommitteeExpense>();
                mainCommExpense.MainCommitteeExpenseID = Guid.NewGuid();
                mainCommExpense.CommitteBankAccountID = bankAccID;
                mainCommExpense.Amount = amount;
                mainCommExpense.ExpenseDate = expenseDate;
                _AdministrationRepository.CreateMainCommitteeExpense(mainCommExpense);

                trans.Commit();
            }
        }
    }
}
